<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'message.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	
	<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/interface/messageAction.js'></script>
	<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/engine.js'></script>
	<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/util.js'></script>	
	<script type='text/javascript'
			src='<%=request.getContextPath()%>/js/forCSS.js'></script>
	<script type="text/javascript">
			function showAddressList(){
			//获取已经选择的节点
			var objSelectedNos = document.getElementById("selectPhoneNos");
			var optionArray = objSelectedNos.options;
			var size = optionArray.length;
			var departs = "";
			var value = null;
			for(var i = 0; i < size; i++){
				value = optionArray[i].value;
				if(value.startWith("t"))
					departs += value + ",";
				else if(value.startWith("d") && value.length == 4){
					departs += value + ",";
				}else if(value.startWith("g"))
					departs += value + ",";
			}
			if(departs.length > 0)
				departs = departs.substring(0,departs.length - 1);			
			window.showModelessDialog("<%=request.getContextPath()%>/message/telephonesearchstudentbyconditions.jsp",window.document,"dialogHeight:768px; dialogWidth:1024px; center:yes; help:no;status:no;");
		}
        var xmlHttpReq = null;		
		function initXmlHttpReq(){
			var args = ["Msxml2.XMLHTTP","Msxml3.XMLHTTP","Microsoft.XMLHTTP"];
			if(xmlHttpReq == null){
				for(var i = 0, j = args.length; i<j; i++){
					xmlHttpReq = new ActiveXObject(args[i]);					
					if(xmlHttpReq)
						return;
				}
				
				xmlHttpReq = new XMLHttpRequest();
			}
		} 
       //点击有效性检测-----------------------------
		function validPhoneNos(){
		
			var phoneFiles = "";
			var params = "";
			var phoneNos = "";
			var obj = document.getElementById("selectPhoneNos");
			var size = obj.options.length;
			var tempValue = null;
			if(size == 0){
				alert("当前没有选择收件人！");
				return;
			}
			for(var i = 0; i < size - 1; i++){
				tempValue = obj.options[i].value;
				if(tempValue.charAt(0) == "f")
					phoneFiles += tempValue.substring(1) + ",";
				else if(tempValue.charAt(0) == "a")
					phoneNos += tempValue.substring(1) + ",";
				else
					phoneNos += tempValue + ",";
			}
			tempValue = obj.options[size - 1].value;
			if(tempValue.charAt(0) == "f")
				phoneFiles += tempValue.substring(1);
			else if(tempValue.charAt(0) == "a")
				phoneNos += tempValue.substring(1);
			else
				phoneNos += tempValue;
			
			if(phoneNos.length > 0 && phoneFiles.length == 0)
				params += "phoneNos=" + phoneNos;
			
			if(phoneFiles.length > 0 && phoneNos.length == 0)
				params += "files=" + phoneFiles;
				
			if(phoneNos.length > 0 && phoneFiles.length > 0)
				params += "phoneNos=" + phoneNos + "&files=" + phoneFiles;
			messageAction.validPhoneNos(params, function (slist){
					if(null == slist){
						return;
					}
					 $('tip').innerHTML = slist;
				
			});
		}
			

	function addNewPhoneNo(){
			var keyCode = window.event ? event.keyCode : event.which;
			var eventObj = event.srcElement || event.target;
			if(keyCode == 13){
				var phoneNo = eventObj.value;
				
				//校验号码的有效性
				phoneNo = delete86(phoneNo);
				
				if(phoneNo == null || phoneNo.length == 0){
					alert("请输入手机号码！");
					return;
				}else if(phoneNo.length != 11){
					alert("请输入合法的手机号码！");
					return;
				}
				
				var obj = document.getElementById("selectPhoneNos");
				var optionObj = document.createElement("<option>");
				optionObj.setAttribute("value",phoneNo);
				var textNode = document.createTextNode(phoneNo);
				optionObj.appendChild(textNode);
				obj.appendChild(optionObj);
				
				eventObj.value = "";
			}
		}
		
		//点击删除收件人
		function delAcceptors(){
			var obj = document.getElementById("selectPhoneNos");
			var size = obj.options.length;
			var optionObj = null;
			var preDelOptions = new Array();
			var index = 0;

			if(size > 0){
				if(obj.value == null || obj.value.length == 0){
					alert("请选择收件人！");
					return;
				}
				
				for(var i = 0; i < size; i++){
					optionObj = obj.options[i];
					if(optionObj.selected){
						preDelOptions[index++] = optionObj;
					}	
				}
				
				for(var i = 0, j = preDelOptions.length; i < j; i++)
					obj.removeChild(preDelOptions[i]);
				
				preDelOptions = null;
				
				setFocus(obj);
			}else{
				alert("当前没有选择收件人！");
			}
		}
		function setFocus(param){
			var size = param.options.length;
			if(size > 0)
				param.options[0].selected = true;
		}
		
			function addNewPhoneNo(){
			var keyCode = window.event ? event.keyCode : event.which;
			var eventObj = event.srcElement || event.target;
			if(keyCode == 13){
				var phoneNo = eventObj.value;
				
				//校验号码的有效性
				phoneNo = delete86(phoneNo);
				
				if(phoneNo == null || phoneNo.length == 0){
					alert("请输入手机号码！");
					return;
				}else if(phoneNo.length != 11){
					alert("请输入合法的手机号码！");
					return;
				}
				
				var obj = document.getElementById("selectPhoneNos");
				var optionObj = document.createElement("<option>");
				optionObj.setAttribute("value",phoneNo);
				var textNode = document.createTextNode(phoneNo);
				optionObj.appendChild(textNode);
				obj.appendChild(optionObj);
				
				eventObj.value = "";
			}
		}
		
		//添加号码
		function addNewPhone(){
			var eventObj = document.getElementById("inputPhoneNo");
			var phoneNo = eventObj.value;
			//校验号码的有效性
			phoneNo = delete86(phoneNo);
				
			if(phoneNo == null || phoneNo.length == 0){
				alert("请输入手机号码！");
				return;
			}else if(phoneNo.length != 11){
				alert("请输入合法的手机号码！");
				return;
			}
				
			var obj = document.getElementById("selectPhoneNos");
			var optionObj = document.createElement("<option>");
			optionObj.setAttribute("value",phoneNo);
			var textNode = document.createTextNode(phoneNo);
			optionObj.appendChild(textNode);
			obj.appendChild(optionObj);
				
			eventObj.value = "";
		}
		
		String.prototype.startWith = function(str){
			if(str == null || str.length == 0 || this.length == 0 || str.length > this.length)
				return false;
			if(this.substr(0,str.length) == str)
				return true;
			else
				return false;
		}
		
		
		function delete86(param){
			if(param.startWith("86"))
				return param.substring(2);
			else if(param.startWith("+86"))
				return param.substring(3);
			else
				return param;
		}
		
		
		function clearTip(){
			var obj = event.srcElement || event.target;
			
			if(obj.value == "输入新的手机号码，回车加入"){
				obj.value = "";
				obj.setAttribute("className","");
			}
		}
		
		function recover(){
			var obj = event.srcElement || event.target;
			if(obj.value == null || obj.value.length == 0){			
				obj.value = "输入新的手机号码，回车加入";
				obj.setAttribute("className","tip");
			}
		}
		
		
		//预览短信
		function previewMsg(){
			//获取短信内容和签名状态
			var previewMsg = "";
			msgCount = 0;
			curIndex = 0;
			msg = new Array();
			var messagecontent = document.getElementById("messagecontent").value;
			if(messagecontent == null || messagecontent.length == 0){
				alert("没有短信内容！");
				return;
			}
			previewMsg += messagecontent;
			var enterpriseName_en = document.getElementById("enterpriseName_en").value;
			var enterpriseName_cn = document.getElementById("enterpriseName_cn").value;
			var isEnglish = true;
			var enterpriseName = enterpriseName_en;
			var enterpriseFlag = document.getElementById("enterpriseFlag").value;
			var nameFlag = false;
			var userName = "";
			
			if(enterpriseFlag != 3){
				for(var i = 0, j = messagecontent.length; i < j; i++)
					if(messagecontent.charCodeAt(i) > 255){
						isEnglish = false;
						enterpriseName = enterpriseName_cn;
						break;
					}
			}
			
			
			
			if(nameFlag)
				previewMsg += "[" + userName + "]";
				
			if(enterpriseFlag != 3)
				previewMsg += "[" + enterpriseName + "]";
			
			var maxLength = 70;
			if(isEnglish){
				maxLength = 160;
			}
			
			var index = 0;
			var msgHtml = "";
			var remainLength = 0;
			var enterpriseLength = enterpriseName.length + 2;
			var totalLength = previewMsg.length;
			var originalLength = 0;
			if(previewMsg.length > maxLength){
				var count = Math.ceil(totalLength / maxLength);
				for(var i = 0; i < count; i++){
					if(i >= count - 2){
						remainLength = previewMsg.substring(i*maxLength).length;
						if(isEnglish){
							originalLength = remainLength - enterpriseLength;
							
							if(originalLength <= 159){
								msgHtml += "<div id='previewMsg'><div>" + previewMsg.substring(i*maxLength, totalLength - (10 + enterpriseLength)) + "</div></div>";
								msgHtml += "<div id='previewMsg'><div>" + previewMsg.substring(totalLength - (10 + enterpriseLength)) + "</div></div>";
							}else if(originalLength == 160){
								msgHtml += "<div id='previewMsg'><div>" + previewMsg.substring(i*maxLength, totalLength - (1 + enterpriseLength)) + "</div></div>";
								msgHtml += "<div id='previewMsg'><div>" + previewMsg.substring(totalLength - (1 + enterpriseLength)) + "</div></div>";
							}else{
								msgHtml += "<div id='previewMsg'><div>" + previewMsg.substring(i*maxLength,(i+1)*maxLength) + "</div></div>";
								msgHtml += "<div id='previewMsg'><div>" + previewMsg.substring((i+1)*maxLength) + "</div></div>";
							} 
						}else{
							if(remainLength < 75 + enterpriseLength){
								msgHtml += "<div id='previewMsg'><div>" + previewMsg.substring(i*maxLength, totalLength - (5 + enterpriseLength)) + "</div></div>";
								msgHtml += "<div id='previewMsg'><div>" + previewMsg.substring(totalLength - (5 + enterpriseLength)) + "</div></div>";
							}else{
								msgHtml += "<div id='previewMsg'><div>" + previewMsg.substring(i*maxLength,(i+1)*maxLength) + "</div></div>";
								msgHtml += "<div id='previewMsg'><div>" + previewMsg.substring((i+1)*maxLength) + "</div></div>";
							}
						}
						
						break;
					}else{
						msgHtml += "<div id='previewMsg'><div>" + previewMsg.substring(i*maxLength,(i+1)*maxLength) + "</div></div>";
					}
				}
				
			}else{
				msgHtml = "<div id='previewMsg'><div>" + previewMsg + "</div></div>";
			}
			
			window.showModalDialog("msgView.jsp",msgHtml,"dialogHeight:455px; dialogWidth:610px; center:yes; help:no;status:no;");
		}
		
		
		//点击发送按钮
		function sendMsg(frmObj){
			//获取收件人
			var objSelectPhoneNos = document.getElementById("selectPhoneNos");
			var size = objSelectPhoneNos.options.length;

			if(size == 0){
				alert("请填写收件人！");
				objSelectPhoneNos.focus();
				return;
			}
			
			var flag = checkMsgInput();
			
			if(flag){
				var receivertelephone = "";
				var optionArray = objSelectPhoneNos.options;
				for(var i = 0; i < size - 1; i++){
					receivertelephone += optionArray[i].value + ",";
				}
				receivertelephone += optionArray[size-1].value;
				document.getElementById("receivertelephone").value=receivertelephone;
				frmObj.submit();
				
			}
		}
	
		
		
		function checkMsgInput(){
			var objmessagecontent = document.getElementById("messagecontent");
			var messagecontent = objmessagecontent.value;
			
			if(messagecontent == null || messagecontent.length == 0){
				alert("请填写短信内容！");
				objmessagecontent.focus();
				return false;
			}else{
				var maxLength = 862;
				for(var i = 0, j = messagecontent.length; i < j; i++){
					if(messagecontent.charCodeAt(i)>255){
						maxLength = 500;
					}
				}
				
				if(messagecontent.length > maxLength){
					alert("短信内容超过最大长度！");
					return false;
				}
			}
			
			return true;
		}
		

		</script>
		
		
  
</head>

<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
		<jsp:include page="messagenav.jsp"></jsp:include>

<div id="page_container">
			<FORM method="post" id="MessageFrm" name="MessageFrm" action="message.action">

		<div id="frame">
		
		<div class="frameborder">
				<table width="100%" border="0" cellspacing="0" cellpadding="0">
		<input type="hidden" name="method" value="sendMsg"/>
		<input type="hidden" name="msgType" value="1"/>
		<input type="hidden" name="acceptors" value=""/>
		<input type="hidden" name="preAcceptor" value="">
		<input type="hidden" name="buttonType" value="2">
		<input type="hidden" name="refreshFlag" value="0">
		<input type="hidden" name="enterpriseFlag" value="1">
		<input type="hidden" name="enterpriseName_en" value="">
		<input type="hidden" name="enterpriseName_cn" value="北科大后勤">
		<input type="hidden" name="userName" value="高级管理员">
		<input type="hidden" name="msgPreview" value="" id="msgPreview">
		<input type="hidden"  name="receivertelephone" id="receivertelephone"/>
		
						
				
					<tr>
						<th align="right" nowrap>
							收件人：
						</th>
						<td align="left" width="72%">
							<div align="left">
								<div>
									<input type="text" name="inputPhoneNo" value="输入新的手机号码，回车加入" onclick="javascript:clearTip();" onkeydown="javascript:addNewPhoneNo();" onblur="javascript:recover();" style="width:180px;" class="tip">
									<input type="button" name="addressList" value="增加手机号" onclick="javascript:addNewPhone();" class="Button">
									<input type="button" name="addressList" value="通讯录" onclick="javascript:showAddressList();" class="Button">
								</div>
								<div align="left">
									<div style="float: left;">
										<select name="selectPhoneNos" multiple="multiple" size="15" style="width:180px"></select>
										<font color="red">*</font>
									</div>
									<div>
										<div style="height: 165px;">
										</div>
										&nbsp;
										<input type="button" name="deleteAcceptors" value="删除收件人" onclick="javascript:delAcceptors();" class="Button">
										<br />
										&nbsp;
										<input type="button" name="checkValidaty" value="有效性检测" onclick="javascript:validPhoneNos();" class="Button">
										</div>
								</div>
								<div id="tip" style="clear: left;">
								</div>
							</div>
						</td>
					</tr>
					<tr height="25">
						<th align="right" width="28%" nowrap>
							短信内容：
						</th>
						<td align="left" width="72%">
							<div style="float: left;">
								<input type="button" value="短信预览"
									onclick="javascript:previewMsg();" class="Button" />
								<br />
								<textarea id="messagecontent" name="messagecontent" cols="60" rows="12"
									></textarea>
								&nbsp;
								<font color="red">*</font> &nbsp;
							</div>
							<div style="clear: left;">
								<font color="red"><script language="javascript">
var submitflag = true;
function checkSmsContext(send_content){
var maxlengths =160;
iList=0;
str = send_content;
iLength = send_content.length;
for(i=0;i<iLength;i++){
if (str.charCodeAt(i)>255){
maxlengths =70;
  break;}}
if (iLength>maxlengths){
if(iLength%maxlengths == 0){iList = parseInt(iLength/maxlengths);} 
else{iList = parseInt(iLength/maxlengths) + 1;}
}else if (iLength>0){
iList=1;}
if(maxlengths==70){
if(iLength >500){
submitflag=false;}
else{
submitflag=true;
}}
else{
if(iLength>862){
submitflag=false;}
else{
submitflag=true;
}}
document.getElementById("sms_contextlimit").innerHTML="系统设置的中文长短信最长为"+500+"个汉字，英文长短信最长为"+862+"英文字母。"+
"您已经输入"+iLength+"字，分为"+iList+"条发送。"; }

setInterval("checkSmsContext(document.getElementById('messagecontent').value)", 100);
</script>
			<div id="sms_contextlimit" class="STYLE3"/>
							</font></div>
							
						</td>
					</tr>
					
					

					
				</table>
				<div class="Submit" align="center">
				
					<input type="button" name="send" value="发送" onclick="javascript:sendMsg(MessageFrm);" class="Button">
					<input type="reset" name="reset" value="重置" class="Button">
				</div>
			</div>
		</div>

		<div id="tipMsg" style="display: none">
		</div>
	</form>
	</div>
</body>
</html>

  
